/*	util.h
*
*   Version 0.4 Changes - 03/18/2007 JR
*   - Added servo_init and servo defines.
*
*   Version 0.3 Changes - 1/02/2007 JR 
*   - No Changes 
*
*   Version 0.2 Changes - 12/03/2006 JR
*   - Fixed some errors in the comments.
*
*/

// Macro definitions
// sbi and cbi are not longer supported by the avr-libc
// to avoid version-conflicts the macro-names have been 
// changed to SetBit and ClearBit.
#define SetBit(sfr, bit) ((sfr) |= (1 << (bit)))
#define ClearBit(sfr, bit) ((sfr) &= ~(1 << (bit)))

#define FALSE   0
#define TRUE    (!FALSE)
#define NULL    0

/* Simplify printing to LCD for students.  */ 
#define printLCD(string)	LCD_puts_f(PSTR(string),1)

void showADCLeftRight(void);
void showADCLineCenter(void);

void CompassDiff(void);
void ThermNoise(void);
void CompNoise(void);
void CompassTest(void);


void RTurn90(void);
void LTurn90(void);





int CheckTerminatingConditions(int TerminatingCondition,unsigned long TerminatingValue);	
// terminating conditions
#define	UNTIL_KEYPRESS				1
#define	UNTIL_RIGHTSENSOR_GREATER	2
#define	UNTIL_MIDDLESENSOR_GREATER	3
#define	UNTIL_LEFTSENSOR_GREATER	4
#define	UNTIL_LINESENSOR_GREATER	5
#define	UNTIL_RIGHTSENSOR_LOWER		6
#define	UNTIL_MIDDLESENSOR_LOWER	7
#define	UNTIL_LEFTSENSOR_LOWER		8
#define	UNTIL_LINESENSOR_LOWER		9
#define	UNTIL_TIME					10

	//stuff that chris made
	
unsigned char OriginalHeading;


void GoToFlame(void);
void FlameSensorTest(void);

/*usart init and PC comunication functions (this is copied from Joe Pardue's Book)*/
void USART_Init(unsigned char);
char isCharAvailable(void);
char receiveChar(void);
void sendChar(char ) ;
void sendNumber(long);
void sendString(char *);

/*Readthermalarray*/
unsigned char	ambient,
				PixelTemperature[8];
int Readthermalarray(unsigned char);
int GetHotPixel(unsigned char *PixelNumber,unsigned char *DeltaTemp);


/*Initialization*/
void Initialization(void);



/*I2C low level functions */

#define	SCL			PE4
#define	SDA			PE5
#define	SCL_Low()	SetBit(DDRE, SCL); SetBit(DDRE, SCL)		// make it an output
#define	SCL_High()	ClearBit(DDRE, SCL); ClearBit(DDRE, SCL)	// make it an input
#define	SDA_Low()	SetBit(DDRE, SDA)							// make it an output
#define SDA_High()	ClearBit(DDRE, SDA)							// make it an input
#define	I2C_TIMEOUT	1000

void	I2C_Init(void);
void	I2C_Start(void);
void	I2C_Stop(void);
int		I2C_Transmit(unsigned char);
int		I2C_Receive(unsigned char *);
/*SetupLineSensors*/
void SetupLineSensors(void);

/* read compass function*/
unsigned char	Heading;
int ReadCompass(void);

/* waits (pauses) for ms milliseconds (assumes internal clock at 1MHz) */
void WaitMs(unsigned int ms);

/* sets up microprocessor for PWM control of motors */
void InitPWM(void);

/* pwm values can range from -10 (full-speed reverse)
   to 10 (full-speed forward), with 0 indicating a stop */
void SetLeftMotorPWM(int pwm);
void SetRightMotorPWM(int pwm);

/* sets up the Analog-to-Digital Converter (ADC) */
void InitADC(void);
/* reads the specified ADC channel (0-7) and returns its value (0-255) */
unsigned char GetADC(char adc);

/* Initializes Timer/Counter 1 for Servo */ 
void init_servo(void);
#define servo_up()			OCR1A = 2000;			// 1.0ms 45 deg ccw from center
#define servo_center()		OCR1A = 3000;			// 1.5ms Center Servo
#define servo_down()		OCR1A = 4000;			// 2.0ms 45 deg cw from center

/* initializes Timer/Counter 0 for timing */
void InitTimer(void);
/* returns number of ms since Timer initialized */
unsigned long TimerMsCur(void);
/* returns true if the specified number of milliseconds
   has passed since the start time (else returns false) */
char TimerCheck(unsigned long msStart, unsigned int msWait);
/* pauses for the specified number of milliseconds */
void TimerWait(unsigned int ms);

#define EEPROMWriteByte(addr, byte) \
	eeprom_write_byte((uint8_t *)(addr), (byte))
#define EEPROMReadByte(addr) \
	eeprom_read_byte((uint8_t *)(addr))


/* Disable JTAG port, making pins available for other use.  */
/* JTAG needs to be disabled for available ADCs to work     */ 
void disable_JTAG(void);

void OSCCAL_calibration(void);
void Delay(unsigned int millisec);

